
/**************************************************************************/
/*                                                                        */
/*  System  Name :  ICIM                                                  */
/*                                                                        */
/*  Description  :  Tool Management                                       */
/*                                                                        */
/*  MODIFICATION HISTORY                                                  */
/*    Date     Ver     Name          Description                          */
/* ---------- ----- ----------- ------------------------------------------ */
/* 2017/09/19 N0.00                     Initial release                   */
/*                                                                        */
/**************************************************************************/

$(document).ready(function() {

    $("form").submit(function(){
        return false;
    });
    
    var inObj_Query;
    var YNBean={
        Value : ['Y','N'],
        Text:{
            Y : 'Y:YES',
            N : 'N:NO'
        }
    };
    
    var domObj = {
    	W : $(window)       ,
        $toolIDTxt : $("#toolIDTxt"),
        $UnitTypeSel : $("#UnitTypeSel"),
        $toolDscTxt  : $("#toolDscTxt"),
        $rootToolIDSel:$("#rootToolIDSel"),
        $toolCateSel : $("#toolCateSel"),
        $bayIDSel  : $("#bayIDSel"),
        $fabId  : $("#fabId"),
        $toolRunModeSel : $("#toolRunModeSel"),
        $toolIP :$("#toolIP"),
        $toolGroupId: $("#toolGroupId"),
        grids : {
            $leftDiv : $("#leftDiv"),
            $tabDiv : $("#tabDiv"),
            $toolListGrd : $("#toolListGrd"),
            $toolListDiv : $("#toolListDiv"),
            $toolListPg : "#toolListPg"
        },
        buttons : {
            $query_btn : $("#query_btn"),
            $delete_btn : $("#delete_btn"),
            $update_btn : $("#update_btn"),
            $add_btn : $("#add_btn"),
            $save_btn : $("#save_btn"),
            $clear_btn : $("#clear_btn"),
            $copy_btn : $("#copy_btn"),
            $rollback_btn : $("#rollback_btn"),
            $toolPt_addBtn : $("#toolPt_addBtn"),
            $toolPt_updateBtn : $("#toolPt_updateBtn"),
            $toolPt_delBtn : $("#toolPt_delBtn")
        },
        dialogs : {
            $queryToolDialog : $("#queryToolDialog"),
            $queryToolDialog_queryBtn : $("#queryToolDialog_queryBtn"),
            $queryToolDialog_toolIDTxt : $("#queryToolDialog_toolIDTxt")
        }
    };
    
    var VAL = {
        DISABLED_ATTR : {
            'disabled' : true
        },
        ENABLED_ATTR : {
            'disabled' : false
        },
        EVT_USR : $("#userId").text(),
        NORMAL : "0000000",
        T_FBPBISTOL:"FBPBISTOL",
        T_FBPBISSTK:"FBPBISSTK"
    };
    
	$("#toolListGrd").jqGrid({
		url:"",
		datatype:"local",
		mtype:"POST",
		shrinkToFit:false,
		scroll:true,
		resizable:true,
		loadonce:true,
		fixed: true,
		viewrecords : true, //显示总记录数
		pager : '#toolListPg',
		rownumbers  :true ,//显示行号
		emptyrecords :true ,
		colModel: [
            {
            	name: 'tool_id'        , 
            	index: 'tool_id'     , 
            	label: TOOL_ID_TAG     , 
            	width:200 
            },{
            	name: 'tool_dsc'       , 
            	index: 'tool_dsc'    , 
            	label: TOOL_DSC_TAG    , 
            	width: 300 
            }],
        onSelectRow:function(id){
        	if(domObj.$toolDscTxt.attr("disabled") != "disabled"){
        		showErrorDialog("", "目前是编辑状态，不可进行其它操作!");
        		return false;
        	}
        	$("input").val("");
        	$("input").attr(VAL.DISABLED_ATTR);
        	$("select").attr(VAL.DISABLED_ATTR);
        	var rowData = $(this).jqGrid("getRowData",id);
        	var tool_id  = rowData.tool_id;
        	var unit_typ="",root_tool_id="", tool_cate="", bay_id="",
        		tool_run_mode="", fab_id_fk="", tool_trns_cate="",tool_code="",fab_site_fk="",tool_group_id_fk="";
        	var iary = {
        			tool_id:tool_id
        	};
        	var inTrxObj ={
        			trx_id : VAL.T_FBPBISTOL,
        			action_flg:"I",
        			iary   : [iary],
        			tbl_cnt: 1
        	};
        	var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
        	if(  outTrxObj.rtn_code == VAL.NORMAL ) {
        		var oary = outTrxObj.oary[0];
        		$("#toolIDTxt").val(oary.tool_id);
        		$("#toolDscTxt").val(oary.tool_dsc);
        		domObj.$toolIP.val(oary.tool_ip);
        		if(oary.unit_typ!=undefined){
        			unit_typ = oary.unit_typ;
        		}
        		SelectDom.setSelect(domObj.$UnitTypeSel, unit_typ);

        		if(oary.root_tool_id!=undefined){
        			root_tool_id = oary.root_tool_id.trim();
        		}
        		SelectDom.setSelect(domObj.$rootToolIDSel, root_tool_id);

        		if(oary.tool_cate!=undefined){
        			tool_cate = oary.tool_cate.trim();
        		}
        		SelectDom.setSelect(domObj.$toolCateSel, tool_cate);

        		if(oary.bay_id!=undefined){
        			bay_id = oary.bay_id;
        		}
        		SelectDom.setSelect(domObj.$bayIDSel, bay_id);

                if(oary.fab_id_fk!=undefined){
                   var fab_id = oary.fab_id_fk;
                }
                SelectDom.setSelect(domObj.$fabId, fab_id);


        		if(oary.tool_run_mode!=undefined){
        			tool_run_mode = oary.tool_run_mode;
        		}
        		SelectDom.setSelect(domObj.$toolRunModeSel, tool_run_mode);
        		
        		if(oary.tool_group_id_fk!=undefined){
        			tool_group_id_fk = oary.tool_group_id_fk;
        		}
        		SelectDom.setSelect(domObj.$toolGroupId,tool_group_id_fk);
                domObj.$toolGroupId.attr(VAL.DISABLED_ATTR);
        		setGridInfo(outTrxObj.oaryB, "#toolPortListGrd");
        	}
        }
	});
	
    //表格自适应
    function resizeFnc(){
        var offsetBottom, divWidth;
        divWidth = $("#toolListDiv").width();
        offsetBottom = $(window).height() -  $("#toolListDiv").offset().top;
        $("#toolListDiv").height(offsetBottom * 0.95);
        $("#toolListGrd").setGridWidth(divWidth * 0.99);
        $("#toolListGrd").setGridHeight(offsetBottom * 0.99 - 101);
    };
    
    $(window).resize(function() {
        resizeFnc();
    });
    
    function com_get_1st_inf(){
        var rowIds = $("#toolListGrd").jqGrid('getDataIDs');
        if(rowIds.length>0){
            $("#toolListGrd").setSelection(rowIds[0], true);
        }
    }
    
    function query_func(){
        function diaLogQueryFnc(){
            var iary = {};
            $("#toolListGrd").jqGrid("clearGridData");
            $("#toolPortListGrd").jqGrid("clearGridData");
            $("#toolPmListGrd").jqGrid("clearGridData");
            var tool_id = $("#queryToolDialog_toolIDTxt").val();
            if( tool_id != "" ){
                iary.tool_id = tool_id
            }
            inObj_Query ={
                trx_id     : VAL.T_FBPBISTOL ,
                action_flg : 'Q'        ,
                iary       : [iary]       ,
                tbl_cnt    : 1
            };
            var  outTrxObj = comTrxSubSendPostJson(inObj_Query);
            if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                setGridInfo(outTrxObj.oary,"#toolListGrd");
                $('#queryToolDialog').modal("hide");
            }
        }
        function queryToolDialogShowFnc(){

        }
        $('#queryToolDialog').modal({
            backdrop:true,
            keyboard:false,
            show:false
        });
        $('#queryToolDialog').unbind('shown.bs.modal');
        $("#queryToolDialog_queryBtn").unbind('click');
        $("#queryToolDialog_toolIDTxt").unbind('keyup');

        $('#queryToolDialog').bind('shown.bs.modal',queryToolDialogShowFnc);
        $('#queryToolDialog').modal("show");
        $("#queryToolDialog_queryBtn").bind('click',diaLogQueryFnc);
        $("#queryToolDialog_toolIDTxt").bind('keyup',function(event){
            if(event.keyCode==13){
                diaLogQueryFnc();
            }
        })
        $("#queryToolDialog_toolIDTxt").attr({'disabled':false});
        $("#queryToolDialog_toolIDTxt").val("");
        $("#queryToolDialog_toolIDTxt").focus();
    }
    
    domObj.$UnitTypeSel.change(function() {
        if($.trim($("#UnitTypeSel").val()) == "SUB"){
        	$("#rootToolIDSel").attr(VAL.ENABLED_ATTR);
            $("#toolGroupId").attr(VAL.DISABLED_ATTR);
            getMainTool();
        }else{
            $("#rootToolIDSel").attr(VAL.DISABLED_ATTR);
            $("#toolGroupId").attr(VAL.ENABLED_ATTR);
            SelectDom.initWithSpace($("#rootToolIDSel"));
        }

    });
    
    //获取MAIN设备
    function getMainTool(){
        var toolg_id = $.trim($("#rootToolIDSel").val());
        var type = $.trim(domObj.$UnitTypeSel.val());
        var unitTyp= "";
        if(type=='SUB'){
            unitTyp="MAIN";
        }else if(type == 'SUB_UNIT'){
            unitTyp= "SUB";
        }
        var iary={
            unit_typ:unitTyp
        };
        var inTrxObj = {
            trx_id     : VAL.T_FBPBISTOL ,
            action_flg : 'Q' ,
            iary       : [iary]       ,
            tbl_cnt    : 1
        }
        var outTrxObj = comTrxSubSendPostJson(inTrxObj);
        if(outTrxObj.rtn_code == VAL.NORMAL) {
            var tbl_cnt= comParseInt(outTrxObj.tbl_cnt);
            var oary = outTrxObj.oary;
            $("#rootToolIDSel").empty();
            $("#rootToolIDSel").append("<option value="+ ""  +">"+ ""+"</option>")
            for(var i=0;i<tbl_cnt;i++){
                $("#rootToolIDSel").append("<option value="+ oary[i].tool_id  +">"+ oary[i].tool_id+"</option>")
            }
            $("#rootToolIDSel").val(toolg_id).trigger("change");
        }
    }
    
    function add_func(){
        if(domObj.$toolDscTxt.attr("disabled") != "disabled"){
        	showErrorDialog("", "目前是编辑状态，不可进行其它操作!");
            return false;
        }
        $("input").attr(VAL.ENABLED_ATTR);
        $("select").attr(VAL.ENABLED_ATTR);
        $("input").val("");
        SelectDom.setSelect(domObj.$UnitTypeSel, "", "");
        SelectDom.setSelect(domObj.$toolCateSel, "", "");
        SelectDom.setSelect(domObj.$toolGroupId, "", "");
        SelectDom.setSelect(domObj.$toolRunModeSel, "", "");
        SelectDom.setSelect(domObj.$rootToolIDSel, "", "");
        SelectDom.setSelect(domObj.$bayIDSel, "", "");
        SelectDom.setSelect(domObj.$fabId, "", "");
        domObj.$toolIDTxt.focus();
        $("#toolPortListGrd").jqGrid("clearGridData");
        $("#save_btn").data("save_flg","A");
    }
    
    function update_func(){
        if(domObj.$toolDscTxt.attr("disabled") != "disabled"){
        	showErrorDialog("", "目前是编辑状态，不可进行其它操作!");
            return false;
        }
        var selRowId = domObj.grids.$toolListGrd.jqGrid("getGridParam","selrow");
        if (!selRowId) {
            showErrorDialog("", "请选择需要修改的设备信息");
            return false;
        }
        $("input").attr(VAL.ENABLED_ATTR);
        $("#toolIDTxt").attr(VAL.DISABLED_ATTR);
        $("select").attr(VAL.ENABLED_ATTR);
        $("#save_btn").data("save_flg","U");
    }
    
    function delete_func(){
        var selectID = $("#toolListGrd").jqGrid("getGridParam","selrow");
        if( selectID==null){
            showErrorDialog("","请选择需要删除的设备");
            return false;
        }
        if(domObj.$toolDscTxt.attr("disabled") != "disabled"){
        	showErrorDialog("", "目前是编辑状态，不可进行其它操作!");
            return false;
        }
        var rowData = $("#toolListGrd").jqGrid("getRowData",selectID);
        var tool_id = rowData.tool_id;
        if( tool_id == ""|| tool_id == undefined ){
            showErrorDialog("","请选择需要删除的设备");
            return false;
        }
        domObj.buttons.$delete_btn.showCallBackWarnningDialog({
            errMsg  : "是否删除设备,请确认!!!!",
            callbackFn : function(data) {
                if(data.result==true){
                    var iary = {
                        tool_id:tool_id
                    };
                    var inTrxObj = {
                        trx_id     : VAL.T_FBPBISTOL ,
                        action_flg : 'D' ,
                        iary       : [iary]       ,
                        tbl_cnt    : 1
                    }
                    var outTrxObj = comTrxSubSendPostJson(inTrxObj);
                    if(outTrxObj.rtn_code == VAL.NORMAL) {
                        SelectDom.setSelect($("select"), "", "");
                        $("input").val("");
                        showSuccessDialog("删除设备成功");
                        $("#toolListGrd").jqGrid("delRowData",selectID);
                    }
                }
            }
        });
    }
    
    function rollback_func(){
        if(domObj.$toolDscTxt.attr("disabled") == "disabled"){
            showErrorDialog("", "没有编辑，无需撤回");
            return false;
        }
        $("#toolDetailForm input").attr(VAL.DISABLED_ATTR);
        $("#toolDetailForm select").attr(VAL.DISABLED_ATTR);
        var outObj="";
        if(inObj_Query!=undefined){
            outObj = comTrxSubSendPostJson(inObj_Query);
            if (outObj.rtn_code === VAL.NORMAL) {
                setGridInfo(outObj.oary, "#toolListGrd");
                com_get_1st_inf();
            }
        }
    }
    
    function clear_func(){
        $("input").val("");
        SelectDom.setSelect($("select"), "", "");
        $("#toolDetailForm input").attr(VAL.DISABLED_ATTR);
        $("#toolDetailForm select").attr(VAL.DISABLED_ATTR);
        $("#toolListGrd").jqGrid("clearGridData");
    }
    
    function copy_func(){
        if(domObj.$toolDscTxt.attr("disabled") != "disabled"){
            showErrorDialog("", "目前处于编辑中，请勿进行其它操作");
            return false;
        }
        var rowid = domObj.grids.$toolListGrd.jqGrid('getGridParam','selrow');
        if(rowid==null){
            showErrorDialog("", "请选择需要复制的设备信息！");
            return false;
        }
        $("input").attr(VAL.ENABLED_ATTR);
        $("select").attr(VAL.ENABLED_ATTR);
        domObj.$toolIDTxt.focus();
        $("#save_btn").data("save_flg","A");
    }
    
    function save_func(){
        var action_flg = $("#save_btn").data("save_flg");
        if(!action_flg){
            return false;
        }
        var tool_id = $.trim($("#toolIDTxt").val());
        if(!tool_id){
            showErrorDialog("", "请输入设备代码！");
            return false;
        }
        
        var tool_dsc = $.trim($("#toolDscTxt").val());
        if(!tool_dsc){
            showErrorDialog("", "请输入设备描述！");
            return false;
        }
        
        var unit_typ = $.trim($("#UnitTypeSel").val());
        if(!unit_typ){
            showErrorDialog("", "请选择设备类型！");
            return false;
        }
        
        var tool_cate = $.trim($("#toolCateSel").val());
        if(!tool_cate){
            showErrorDialog("", "请选择设备属性！");
            return false;
        }
        
        var toolg_id = domObj.$toolGroupId.val();
        var root_tool_id = $.trim($("#rootToolIDSel").val());
        var tool_run_mode = $.trim($("#toolRunModeSel").val());
        var bay_id = $.trim($("#bayIDSel").val());
        var fab_id = $.trim($("#fabId").val());
        var tool_ip = $.trim(domObj.$toolIP.val());
        
        var iary ={
            tool_id        : tool_id,
            tool_dsc       : tool_dsc,
            unit_typ       : unit_typ,
            tool_cate      : tool_cate,
            tool_group_id_fk : toolg_id,
            root_tool_id   : root_tool_id,
            tool_run_mode : tool_run_mode,
            bay_id         : bay_id,
            fab_id_fk         : fab_id,
            tool_ip        : tool_ip
        };

        if(unit_typ == "MAIN"){
            iary.root_tool_id = tool_id;
            if(!toolg_id){
                showErrorDialog("", "请选择设备组代码！");
                return false;
            }
//            if(!bay_id){
//                showErrorDialog("", "请选择(线别)区域代码！");
//                return false;
//            }
            if(!tool_run_mode){
                showErrorDialog("", "请选择设备运行模式！");
                return false;
            }
        }else if(unit_typ == "SUB" || unit_typ == "UNIT"){
            if(!root_tool_id){
                showErrorDialog("", "请选择上级设备代码！");
                return false;
            }
        }
        
        var inTrxObj = {
            trx_id     : VAL.T_FBPBISTOL ,
            action_flg : action_flg ,
            iary       : [iary]     ,
            evt_usr    : VAL.EVT_USR,
            iaryB      : [],
            tbl_cnt    : 1
        };
        var outTrxObj = comTrxSubSendPostJson(inTrxObj);
        if(outTrxObj.rtn_code == VAL.NORMAL) {
            $("input").attr(VAL.DISABLED_ATTR);
            $("select").attr(VAL.DISABLED_ATTR);
            if(action_flg=="A"){
                showSuccessDialog("新增设备信息成功");
                var newRowID = getGridNewRowID("#toolListGrd");
                var oary = iary;
                $("#toolListGrd").jqGrid('addRowData',newRowID,iary);
                $("#toolListGrd").setSelection(newRowID, true);
            }else if(action_flg=="U"){
                showSuccessDialog("设备信息更新成功");
            }
        }
    }
    
    function initFnc(){
        $("input").attr(VAL.DISABLED_ATTR);
        $("select").attr(VAL.DISABLED_ATTR);
        $("input").val("");
        $("select").empty();
        comAddValueByDataCateFnc("#UnitTypeSel","EQTP","data_ext","data_desc",true);
        comAddValueByDataCateFnc("#toolCateSel","EQCT","data_ext","data_desc",true);
        comAddValueByDataCateFnc("#bayIDSel","AREA","data_ext","data_desc",true);
        comAddValueByDataCateFnc("#fabId","FBID","data_ext","data_desc",true);
        comAddValueByDataCateFnc("#toolRunModeSel","TRMD","data_ext","data_desc",true);
        comAddValueByDataCateFnc("#toolGroupId","TOLG","data_ext","data_desc",true);
    }
    
    domObj.buttons.$update_btn.click(update_func);
    domObj.buttons.$add_btn.click(add_func);
    domObj.buttons.$delete_btn.click(delete_func);
    domObj.buttons.$clear_btn.click(clear_func);
    domObj.buttons.$query_btn.click(query_func);
    domObj.buttons.$save_btn.click(save_func);
    domObj.buttons.$rollback_btn.click(rollback_func);
    domObj.buttons.$copy_btn.click(copy_func);
    
    resizeFnc();
    initFnc();
    //enter
    /*document.onkeydown=function(event){
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if(e && e.keyCode==13){ // 按 enter
            $('.bootbox').modal("hide");
        }
    }*/
});